<?php
declare (strict_types=1);

namespace app\admin\logic\finance;

use app\admin\logic\system\Log;
use app\admin\model\finance\Recharge as RechargeModel;
use app\common\exception\LogicException;

/**
 * 充值配置模型
 */
class Recharge extends RechargeModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @return   [type]                         [description]
     */
    public function getList(array $map = [])
    {
        $map['mhm_id'] = request()->mhm_id;

        return $this->where($map)->order('update_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * 创建配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @param array $data [description]
     * @return   [type]                               [description]
     */
    public function createConfig(array $data, $is_ios = 0)
    {
        $check = [
            'mhm_id'       => request()->mhm_id,
            'is_ios'       => $is_ios,
            'price'        => $data['price'],
            'balance'      => $data['balance'],
            'give_balance' => $data['give_balance'],
            'product_id'   => $data['product_id'] ?? null,
        ];

        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($check);

        // 创建成功
        if (!$this->save($check)) {
            throw new LogicException('创建失败');
        }

        Log::write('新建' . ($is_ios ? "IOS内购" : "通用充值") . '配置', 'recharge', 'create', true);

        // 返回值
        return $this;
    }

    /**
     * 编辑配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @param    [type]                         $id   [description]
     * @param    [type]                         $data [description]
     * @return   [type]                               [description]
     */
    public function editConfig($id, $data, $is_ios = 0)
    {

        $recharge = $this->where('id', $id)->findOrFail();

        $check           = $data;
        $check['id']     = $id;
        $check['is_ios'] = $is_ios;
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($check);
        if ($is_ios) {
            $recharge->product_id = $data['product_id'];
        }

        $recharge->price        = $data['price'];
        $recharge->balance      = $data['balance'];
        $recharge->give_balance = $data['give_balance'];
        if (!$recharge->save()) {
            throw new LogicException('修改失败');
        }

        Log::write('编辑' . ($is_ios ? "IOS内购" : "通用充值") . '配置/' . $id, 'recharge', 'update', true);

        return $recharge;
    }

    /**
     * 删除配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function deleteConfig($id)
    {
        $recharge = $this->where('id', $id)->findOrFail();

        if (!$recharge->delete()) {
            throw new LogicException("删除失败");
        }
        Log::write('删除' . ($recharge->is_ios ? "IOS内购" : "通用充值") . '配置/' . $id, 'recharge', 'delete', true);
    }
}
